package com.yida.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.constant.UserConstants;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.system.api.domain.SysUser;
import com.yida.system.api.domain.SysUserCommunity;
import com.yida.system.api.domain.request.BindCommunityRequest;
import com.yida.system.api.domain.request.RegisterBindRequest;
import com.yida.system.api.domain.request.SysUserOutsideOfficerRemoveRequest;
import com.yida.system.api.domain.request.SysUserQueryRequest;
import com.yida.system.api.vo.OfficerUserVO;
import com.yida.system.service.ISysUserOutsideOfficerService;
import com.yida.system.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Author: PPV
 * @Date: 2023-03-08
 */
@RestController
@RequestMapping("/outsideOfficer")
@Api(tags = "物业、街道办用户")
public class SysUserOutsideOfficerController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Resource
    private ISysUserOutsideOfficerService officerService;

    @ApiOperation(value = "获取用户列表", response = OfficerUserVO.class)
    @GetMapping("/list")
    @RequiresPermissions("system:outside:officer:list")
    public TableDataInfo list(SysUserQueryRequest request) {
        return getDataTable(officerService.getPage(request));
    }

    @ApiOperation("绑定项目")
    @PostMapping("/bindCommunity")
    @RequiresPermissions("system:outside:officer:edit")
    public AjaxResult bindCommunity(@RequestBody @Valid BindCommunityRequest request) {
        officerService.bindCommunity(request);
        return AjaxResult.success();
    }

    @ApiOperation("查询已绑定项目")
    @GetMapping("/listCommunities/{userId}")
    @RequiresPermissions("system:outside:officer:list")
    public AjaxResult getBindCommunities(@PathVariable("userId") Long userId) {
        return AjaxResult.success(officerService.getBindCommunities(userId));
    }

    @ApiOperation("注册用户并绑定项目")
    @PostMapping("/registerBind")
    @RequiresPermissions("system:outside:officer:save")
    public AjaxResult registerAndBind(@RequestBody RegisterBindRequest request) {
        SysUser user = request.getUserInfo();
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，其它地区已存在相同登录账号");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        officerService.insertOustidUser(request);
        return AjaxResult.success();
    }

    @ApiOperation("删除用户")
    @PostMapping("/removeOutsideUser")
    @RequiresPermissions("system:outside:officer:remove")
    public AjaxResult removeOutsideUser(@RequestBody @Validated SysUserOutsideOfficerRemoveRequest request) {
        officerService.removeOutsideUser(request);
        return AjaxResult.success();
    }

    @InnerAuth
    @ApiOperation(value = "内部获取用户列表", response = OfficerUserVO.class)
    @GetMapping("/innerList")
    public R<TableDataInfo> innerList(SysUserQueryRequest request) {
        return R.ok(getDataTable(officerService.getPage(request)));
    }

    @InnerAuth
    @ApiOperation("内部绑定项目")
    @PostMapping("/innerBindCommunity")
    public R innerBindCommunity(@RequestBody @Valid BindCommunityRequest request) {
        officerService.bindCommunity(request);
        return R.ok();
    }

    @InnerAuth
    @ApiOperation("内部查询已绑定项目")
    @GetMapping("/innerListCommunities/{userId}")
    public R<List<SysUserCommunity>> innerGetBindCommunities(@PathVariable("userId") Long userId) {
        return R.ok(officerService.getBindCommunities(userId));
    }

    @InnerAuth
    @ApiOperation("内部注册用户并绑定项目")
    @PostMapping("/innerRegisterBind")
    public R innerRegisterAndBind(@RequestBody RegisterBindRequest request) {
        officerService.insertOustidUser(request);
        return R.ok();
    }

    @InnerAuth
    @ApiOperation("内部删除用户")
    @PostMapping("/innerRemoveOutsideUser")
    public R innerRemoveOutsideUser(@RequestBody @Validated SysUserOutsideOfficerRemoveRequest request) {
        officerService.removeOutsideUser(request);
        return R.ok();
    }
}
